using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using Syncfusion.Windows.Forms.Grid;
using Syncfusion.Windows.Forms.Tools;

namespace ctsu.CustomControls
{
	public class UserGrid : System.Windows.Forms.UserControl
	{
		public delegate void GridBLRowCountChangeEventHandler(object sender, GridRowCountChangeEventArgs e);

		private Syncfusion.Windows.Forms.Tools.GradientPanel pnlGrid;
		public System.Windows.Forms.Button NewRow;
		public System.Windows.Forms.Button RemoveRow;
		private System.Windows.Forms.ToolTip toolTip1;
		private Syncfusion.Windows.Forms.Grid.GridControl _grid;
		private System.Windows.Forms.Label errCtl;
		private System.ComponentModel.IContainer components;
				
		public event GridBLRowCountChangeEventHandler GridRowCountChange;
		
		public GridControl Grid
		{
			get{return _grid;}
		}

		public GradientPanel Panel
		{
			get{return pnlGrid;}
		}

		public UserGrid()
		{			
			InitializeComponent();
		}
		
		public Label ErrorControl
		{
			get{return errCtl;}
		}

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if( components != null )
					components.Dispose();
			}
			base.Dispose( disposing );
		}

	

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            this.components = new System.ComponentModel.Container();
            Syncfusion.Windows.Forms.Grid.GridCellInfo gridCellInfo1 = new Syncfusion.Windows.Forms.Grid.GridCellInfo();
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(UserGrid));
            this.pnlGrid = new Syncfusion.Windows.Forms.Tools.GradientPanel();
            this.errCtl = new System.Windows.Forms.Label();
            this._grid = new Syncfusion.Windows.Forms.Grid.GridControl();
            this.NewRow = new System.Windows.Forms.Button();
            this.RemoveRow = new System.Windows.Forms.Button();
            this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
            ((System.ComponentModel.ISupportInitialize)(this.pnlGrid)).BeginInit();
            this.pnlGrid.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this._grid)).BeginInit();
            this.SuspendLayout();
            // 
            // pnlGrid
            // 
            this.pnlGrid.BackColor = System.Drawing.SystemColors.Control;
            this.pnlGrid.BackgroundColor = new Syncfusion.Drawing.BrushInfo(Syncfusion.Drawing.GradientStyle.PathEllipse, System.Drawing.Color.LightSkyBlue, System.Drawing.SystemColors.Control);
            this.pnlGrid.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
            this.pnlGrid.Border3DStyle = System.Windows.Forms.Border3DStyle.Flat;
            this.pnlGrid.BorderColor = System.Drawing.Color.Black;
            this.pnlGrid.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.pnlGrid.Controls.Add(this.errCtl);
            this.pnlGrid.Controls.Add(this._grid);
            this.pnlGrid.Controls.Add(this.NewRow);
            this.pnlGrid.Controls.Add(this.RemoveRow);
            this.pnlGrid.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pnlGrid.IgnoreThemeBackground = true;
            this.pnlGrid.Location = new System.Drawing.Point(0, 0);
            this.pnlGrid.Name = "pnlGrid";
            this.pnlGrid.Size = new System.Drawing.Size(184, 104);
            this.pnlGrid.TabIndex = 0;
            this.pnlGrid.ThemesEnabled = true;
            // 
            // errCtl
            // 
            this.errCtl.BackColor = System.Drawing.Color.Transparent;
            this.errCtl.Location = new System.Drawing.Point(20, 7);
            this.errCtl.Name = "errCtl";
            this.errCtl.Size = new System.Drawing.Size(8, 8);
            this.errCtl.TabIndex = 137;
            // 
            // _grid
            // 
            this._grid.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this._grid.BackColor = System.Drawing.SystemColors.Control;
            this._grid.ColCount = 0;
            this._grid.DefaultRowHeight = 20;
            this._grid.Font = new System.Drawing.Font("Tahoma", 9.75F);
            gridCellInfo1.Col = -1;
            gridCellInfo1.Row = -1;
            gridCellInfo1.StyleInfo.Font.Bold = false;
            gridCellInfo1.StyleInfo.Font.Facename = "Tahoma";
            gridCellInfo1.StyleInfo.Font.Italic = false;
            gridCellInfo1.StyleInfo.Font.Size = 9.75F;
            gridCellInfo1.StyleInfo.Font.Strikeout = false;
            gridCellInfo1.StyleInfo.Font.Underline = false;
            gridCellInfo1.StyleInfo.Font.Unit = System.Drawing.GraphicsUnit.Point;
            this._grid.GridCells.AddRange(new Syncfusion.Windows.Forms.Grid.GridCellInfo[] {
            gridCellInfo1});
            this._grid.Location = new System.Drawing.Point(48, 8);
            this._grid.Name = "_grid";
            this._grid.RowCount = 0;
            this._grid.Size = new System.Drawing.Size(128, 88);
            this._grid.SmartSizeBox = false;
            this._grid.TabIndex = 136;
            // 
            // NewRow
            // 
            this.NewRow.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.NewRow.Image = ((System.Drawing.Image)(resources.GetObject("NewRow.Image")));
            this.NewRow.Location = new System.Drawing.Point(8, 20);
            this.NewRow.Name = "NewRow";
            this.NewRow.Size = new System.Drawing.Size(32, 23);
            this.NewRow.TabIndex = 134;
            this.toolTip1.SetToolTip(this.NewRow, "Add New Row");
            this.NewRow.Click += new System.EventHandler(this.NewRow_Click);
            // 
            // RemoveRow
            // 
            this.RemoveRow.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.RemoveRow.Image = ((System.Drawing.Image)(resources.GetObject("RemoveRow.Image")));
            this.RemoveRow.Location = new System.Drawing.Point(8, 52);
            this.RemoveRow.Name = "RemoveRow";
            this.RemoveRow.Size = new System.Drawing.Size(32, 23);
            this.RemoveRow.TabIndex = 135;
            this.toolTip1.SetToolTip(this.RemoveRow, "Remove Selected Row");
            this.RemoveRow.Click += new System.EventHandler(this.RemoveRow_Click);
            // 
            // UserGrid
            // 
            this.BackColor = System.Drawing.SystemColors.Window;
            this.Controls.Add(this.pnlGrid);
            this.Name = "UserGrid";
            this.Size = new System.Drawing.Size(184, 104);
            ((System.ComponentModel.ISupportInitialize)(this.pnlGrid)).EndInit();
            this.pnlGrid.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this._grid)).EndInit();
            this.ResumeLayout(false);

		}
		#endregion

		#region button click events
		private void NewRow_Click(object sender, System.EventArgs e)
		{
			GridRowCountChangeEventArgs eargs = new GridRowCountChangeEventArgs(Grid.RowCount-1, GridRowChangeType.New);
			if(GridRowCountChange != null)
				GridRowCountChange(this, eargs);
		}

		private void RemoveRow_Click(object sender, System.EventArgs e)
		{
			GridCurrentCell cc = Grid.CurrentCell;
						
			GridRowCountChangeEventArgs eargs = new GridRowCountChangeEventArgs(cc.RowIndex-1, GridRowChangeType.Remove);
			if(GridRowCountChange != null)
				GridRowCountChange(this, eargs);			
		}
		#endregion button click events
	}

	public enum GridRowChangeType
	{		
		New,		
		Remove
	}

	public class GridRowCountChangeEventArgs
	{		
		public int Row;
		public GridRowChangeType RowChangeType;

		public GridRowCountChangeEventArgs(int row, GridRowChangeType rowChangeType)
		{			
			this.Row = row;			
			this.RowChangeType = rowChangeType;
		}
	}
	
}
